<?php
	if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Admin_Index_Ctrl extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
		}
		
		function index(){			
			$this->load->view('admin/admin_login');
		}
		
		function admin(){
			$url = CURL_URL;
			$data = array('www-command'=>'adminuser-login','user_id'=>$this->input->post('admin_user'),'password'=>$this->input->post('admin_pass'));
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response = json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response);exit;
			$admin_lg = (array)$response->result[0];
			//echo '<pre>';print_r($admin_lg);exit;
			$this->session->set_userdata($admin_lg);
			//echo '<pre>';print_r($this->session->userdata('admin_id'));exit;
			$this->load->view('admin/admin_index_view');
		}
		
		function admin_logout(){
			$this->session->sess_destroy();
			$this->load->view('admin/admin_login');
		}
		
		function table_editable($order_by  = 'created_date', $sort_order = 'asc', $current_page = 0){
			
			
			$results_per_page = 4;
			
			$url = 'http://localhost/azindia/azindia-api/json.api';
			$data = array('www-command'=>'articles-all_types','current_page'=>$current_page,'results_per_page'=>$results_per_page,'sort_order'=>$sort_order,'order_by'=>$order_by);
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response = json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
			
			$this->load->library('pagination');			
			$config['base_url'] = 'http://localhost/azindia/azindia-client/index.php/admin/admin_index_ctrl/table_editable/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows']  = $response->total;
			$config['uri_segment'] = 6;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['articles_all_types'] = $response;
			$data['articles_type_fileds'] = array('title'=>'Article Type Name', 'description '=>'Description ', 'status'=>'Status');
			$data['order_by'] = $order_by;
			$data['sort_order'] = $sort_order;
			
			$this->load->view('admin/table_editable',$data);
		}
		
		
		function create_article_page(){
			$this->load->view('admin/create_article_type');
		}
		
		function create_articles(){
				$this->form_validation->set_error_delimiters('<span style="color:red">', '</span>');
				$this->form_validation->set_rules('title', 'Title', 'required|alpha|min_length[3]|max_length[15]');
				$this->form_validation->set_rules('description', 'Description', 'max_length[255]');
				
				if ($this->form_validation->run() == FALSE)
			{
				$this->load->view('admin/create_article_type');
				
			}else{
				
				$url = 'http://localhost/azindia/azindia-api/en/json.api';
				$data = array('www-command'=>'articles-create_type','title'=>$this->input->post('title'),'description'=>$this->input->post('description'));
				$string = http_build_query($data);
				
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				redirect('admin/admin_index_ctrl/table_editable');
			}	
		}
		
		function edit_article_page(){
				$url = 'http://localhost/azindia/azindia-api/en/json.api';				
				$data = array('www-command'=>'articles-get_article_type','article_type_id'=>$this->uri->segment(4));
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				$response = (array) $response;
				foreach($response as $value){
					$value = $value;
				}
				$data['title'] = $value->title;
				$data['description'] = $value->description;
				$data['article_type_id'] = $value->article_type_id;
				$this->load->view('admin/edit_article_type',$data);
		}
		
		function edit_articles(){
			$this->form_validation->set_error_delimiters('<span style="color:red">', '</span>');
				$this->form_validation->set_rules('title', 'Title', 'required|alpha|min_length[3]|max_length[15]');
				$this->form_validation->set_rules('description', 'Description', 'max_length[255]');
				
				if ($this->form_validation->run() == FALSE)
			{
				$data['title'] = $this->input->post('title');
				$data['description'] = $this->input->post('description');
				$data['article_type_id'] = $this->input->post('hidden');
				$this->load->view('admin/edit_article_type',$data);
				
			}else{
				$url = 'http://localhost/azindia/azindia-api/en/json.api';				
				$data = array('www-command'=>'articles-update_type','title'=>$this->input->post('title'),'description'=>$this->input->post('description'),'article_type_id'=>$this->input->post('hidden'));
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				redirect('admin/admin_index_ctrl/table_editable');
			}
		}
		
		function delete_article(){
			$url = 'http://localhost/azindia/azindia-api/en/json.api';
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'articles-update_type_status','status'=>$status,'article_type_id'=>$this->uri->segment(4));
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				
				redirect('admin/admin_index_ctrl/table_editable');
		}

		
	}
	
?>